package com.change.zh.ffmpegdo.push;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


public class JobSingleDB {

    private final static class SingleHander {
        private final static JobSingleDB INSTANCE = new JobSingleDB(16);
    }

    private JobSingleDB() {
    }

    public static JobSingleDB Instance() {
        return JobSingleDB.SingleHander.INSTANCE;
    }

    // 存放任务信息
    private ConcurrentMap<String, JobDetail> map = null;

    private JobSingleDB(int size) {
        this.map = new ConcurrentHashMap<>(size);
    }

    public JobDetail get(String id) {
        return map.get(id);
    }

    public Collection<JobDetail> getAll() {
        return map.values();
    }

    public int add(JobDetail jobDetail) {
        String id = jobDetail.getId();
        if (id != null && !map.containsKey(id)) {
            map.put(jobDetail.getId(), jobDetail);
            if (map.get(id) != null) {
                return 1;
            }
        }
        return 0;
    }

    public int remove(String id) {
        if (map.remove(id) != null) {
            return 1;
        }
        ;
        return 0;
    }

    public int removeAll() {
        int size = map.size();
        try {
            map.clear();
        } catch (Exception e) {
            return 0;
        }
        return size;
    }

    public boolean isHave(String id) {
        return map.containsKey(id);
    }
}
